package D12_12z;

import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Demo1 {
    static ArrayList<Classes> classe = new ArrayList<>();
    public static void main(String[] args) throws Exception {
      init();
        xq3();
        System.out.println("=======================================================");
        xq4();
        System.out.println("=======================================================");
        xq5();
        xq6();
    }
    public static void xq3(){
        int a = 0,index = 0;
        for (int i = 0; i < classe.size(); i++) {
            int age = 0;
            for (Student student : classe.get(i).getStudents()) {
                age+=student.getAge();
            }
            age = age / classe.get(i).getStudents().size();
            if (age>a){
                a = age;
                index = i;
            }
        }
        System.out.println("班级编号\t班级名称\t讲师\t助理讲师\t   辅导员\t平均年龄");
        System.out.println(classe.get(index).getClssID()+"\t\t"+classe.get(index).getClassname()+"\t\t"+classe.get(index).getTeacher()+"\t\t"+classe.get(index).getAssistant()+"\t\t"+classe.get(index).getInstructor()+"\t\t"+a);
    }
    public static void xq4(){
        int a = 0,index = 0;
        int b = 0,index1 = 0;
        System.out.println("未成年人数\t班级名称");
        for (int i = 0; i < classe.size(); i++) {
            int cj = 0,rencount = 0,wcncount = 0;
            for (Student student : classe.get(i).getStudents()) {
                cj+=student.getMath();
                if (student.getMath()<60){
                    rencount++;
                }
                if (student.getAge()<18){
                    wcncount++;
                }
            }
            if (a==0){
                a=cj;
            }
            cj = cj / classe.get(i).getStudents().size();
            if (cj<a){
                a = cj;
                index = i;
            }
            if (rencount>b){
                b = rencount;
                index1 = i;
            }
            System.out.println(wcncount+"\t\t\t"+classe.get(i).getClassname());
        }
        System.out.println("=======================================================");
        System.out.println("班级编号\t班级名称\t成绩");
        System.out.println(classe.get(index).getClssID()+"\t\t"+classe.get(index).getClassname()+"\t\t"+a);
        System.out.println("=======================================================");
        System.out.println("班级编号\t班级名称\t不及格人数");
        System.out.println(classe.get(index1).getClssID()+"\t\t"+classe.get(index1).getClassname()+"\t\t"+b);
    }
    public static void xq5(){
        for (Classes classes : classe) {
            classes.getStudents().sort(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    if (o1.getAge()-o2.getAge()<0){
                        return -1;
                    }
                    if (o1.getAge()-o2.getAge()==0 && o1.getMath()-o2.getMath()<0){
                        return -1;
                    }
                    return 0;
                }
            });
        }
    }
    public static void xq6() throws IOException {
        for (int i = 0; i < classe.size(); i++) {
            for (int i1 = 0; i1 < classe.get(i).getStudents().size(); i1++) {
                FileUtils.writeStringToFile(new File(classe.get(i).getClssID()+".txt"),classe.get(i).getStudents().get(i1).getNumber()+","+classe.get(i).getStudents().get(i1).getName()+","+
                        classe.get(i).getStudents().get(i1).getAge()+","+classe.get(i).getStudents().get(i1).getGender()+","+classe.get(i).getStudents().get(i1).getMath()+","+classe.get(i).getStudents().get(i1).getClassID()+"\r\n",true);
            }
        }
    }
    public static void init() throws Exception {
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read("src/Y2019/M12_/D12_12z/classes.xml");
        Document read1 = saxReader.read("src/Y2019/M12_/D12_12z/student.xml");
        Element root = read.getRootElement();
        Element root2 = read1.getRootElement();
        List<Element> classes = root.elements();
        List<Element> student = root2.elements();
        for (Element aClass : classes) {
            ArrayList<Student> arr = new ArrayList<>();
            Classes classes1 = new Classes();
            classes1.setClssID(aClass.attributeValue("classID"));
            classes1.setClassname(aClass.attributeValue("classname"));
            classes1.setTeacher(aClass.attributeValue("teacher"));
            classes1.setAssistant(aClass.attributeValue("assistant"));
            classes1.setInstructor(aClass.attributeValue("instructor"));
            for (Element element : student) {
                if (aClass.attributeValue("classID").equals(element.attributeValue("classID"))){
                    Student student1 = new Student();
                    student1.setNumber(element.attributeValue("number"));
                    student1.setName(element.attributeValue("name"));
                    student1.setAge(Integer.parseInt(element.attributeValue("age")));
                    student1.setGender(element.attributeValue("gender"));
                    student1.setMath(Integer.parseInt(element.attributeValue("math")));
                    student1.setClassID(element.attributeValue("classID"));
                    student1.setClasses(classes1);
                    arr.add(student1);
                    classes1.setStudents(arr);
                }
            }
            classe.add(classes1);
        }
    }
}
